package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.dao.CartDao;
import com.example.springboot.dao.GoodsDao;
import com.example.springboot.entity.Cart;
import com.example.springboot.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.lang.Dict;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartDao cartDao;
    private GoodsDao goodsDao;

    @Resource
    private RedisTemplate redisTemplate;

    public CartController(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @GetMapping()
    public Result findByUserid(@RequestParam("userid") String userid){
        return Result.success(cartDao.findByUserid(userid));
    }

    @PostMapping
    public Result Save(@RequestBody Cart cart){
//        System.out.println(cart.getUser_id());
        if(cart.getGoods_id()==null||cart.getUser_id()==null){
            return Result.error("参数为空");
        }
        return Result.success(cartDao.insert(cart));
    }

    @GetMapping ("/personalCart")
    public Result find(@RequestParam("userid") String userid){
        BigDecimal total = new BigDecimal(0);
//        List<Cart> list;
//        if(redisTemplate.hasKey("personalCart:"+userid)){
//
//            System.out.println("1  "+redisTemplate.opsForHash().entries("personalCart:"+userid));
//            list = null;
//        }else{
//            list = cartDao.findByUserid(userid);
//            System.out.println("2  "+list);
//            for(Cart cart:list){
//                redisTemplate.opsForHash().put("personalCart:"+userid,"id",cart.getId());
//                System.out.println(redisTemplate.opsForHash().get("personalCart:"+userid,"id"));
//                redisTemplate.opsForHash().put("personalCart:"+userid,"goods_id",cart.getGoods_id());
//                redisTemplate.opsForHash().put("personalCart:"+userid,"num",cart.getNum());
//                redisTemplate.opsForHash().put("personalCart:"+userid,"user_id",cart.getUser_id());
//            }
//        }
        List<Cart> list = cartDao.findByUserid(userid);
        for(Cart cart: list){
            Goods goods = goodsDao.getById(cart.getGoods_id());
            cart.setGoodName(goods.getName());
            cart.setImg(goods.getImg());
            cart.setPrice(goods.getPrice());
            total = total.add(goods.getPrice().multiply(BigDecimal.valueOf(cart.getNum())));
//            System.out.println(cart.getUser_id());
        }
        Dict dict = Dict.create().set("list", list).set("total", total);
        return Result.success(dict);
    }

    @PostMapping("/cal")
    public Result cal(@RequestBody List<Cart> carts) {
        BigDecimal total = new BigDecimal(0);
        for (Cart cart : carts) {
            Goods goods = goodsDao.getById(cart.getGoods_id());
            total = total.add(goods.getPrice().multiply(BigDecimal.valueOf(cart.getNum())));
        }
        return Result.success(total);
    }

    @DeleteMapping("/{id}")
    public Result Delete(@PathVariable Integer id){
        if(id==null||id==0){
            //throw new RuntimeException("参数不可为空");
            Result.error("参数不可为空");
        }
        return Result.success(cartDao.deleteById(id)==1);
    }
}
